package com.zhc.dejingapi.utils.qing;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.gson.Gson;
import com.zhc.dejingapi.entity.QingAppfieldsEntity;
import com.zhc.dejingapi.mapper.QingAppfieldsMapper;
import com.zhc.dejingapi.mapper.QingAppfieldtablevaluesMapper;
import com.zhc.dejingapi.pojo.param.QingCommonBackResult;
import com.zhc.dejingapi.pojo.qingflow.QingflowApiInfo;
import com.zhc.dejingapi.pojo.qingflow.appdata.adddata.QingAddDataResultMain;
import com.zhc.dejingapi.pojo.qingflow.appdata.editdata.QingEditDataFieldModel;
import com.zhc.dejingapi.pojo.qingflow.appdata.editdata.QingEditDataFieldValueModel;
import com.zhc.dejingapi.pojo.qingflow.appdata.editdata.QingEditDataModel;
import com.zhc.dejingapi.pojo.qingflow.queryresult.QingQueryActionResultBase;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class CommonAppFieldValueUpdateUtils {

    public CommonAppFieldValueUpdateUtils(QingflowApiInfo qqingflowApiInfo1, JdbcTemplate mysqlJdbcTemplate1,
                                          QingAppfieldsMapper qingAppfieldsMapper1,
                                          QingAppfieldtablevaluesMapper qingAppfieldTableValuesMapper1,
                                          String strAppKey,
                                          List<String> listUpdFields

                              )throws Exception{
        this.qqingflowApiInfo=qqingflowApiInfo1;
        this.mysqlJdbcTemplate=mysqlJdbcTemplate1;
        this.qingAppfieldsMapper=qingAppfieldsMapper1;
        this.qingAppfieldtablevaluesMapper = qingAppfieldTableValuesMapper1;
        appKey=strAppKey;
        LoadInfo(listUpdFields);
    }



    private String appKey;
    private JdbcTemplate mysqlJdbcTemplate;
    private QingflowApiInfo qqingflowApiInfo;

    private QingAppfieldsMapper qingAppfieldsMapper;

    private QingAppfieldtablevaluesMapper qingAppfieldtablevaluesMapper;


    private List<QingAppfieldsEntity> listFields=null;
    private void LoadInfo(List<String> listUpdFields) throws Exception{



        QueryWrapper<QingAppfieldsEntity> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("appKey",appKey)
                .gt("queId",1000000)
                .in("queTitle",listUpdFields);




        listFields=qingAppfieldsMapper.selectList(queryWrapper);
        if (listFields==null||listFields.isEmpty()){
            throw new Exception("没有找到对应的字段信息");
        }


    }


    public QingCommonBackResult UploadFieldValue(String strQingAppId,
                                                 Map<String,String> mapFieldValue)
    {
        QingCommonBackResult commonBackResult=new QingCommonBackResult();
        boolean isSuccess=false;
        try{
            //String strAccessToken=accessTokenUtils.getAccessToken(qqingflowApiInfo);

            QingEditDataModel qingEditDataModel =new QingEditDataModel();

            List<QingEditDataFieldModel> listFieldModels=new ArrayList<>();
            for (int i = 0; i < listFields.size(); i++) {
                QingAppfieldsEntity sourceEntity=listFields.get(i);
                QingEditDataFieldModel addDataFieldModel=new QingEditDataFieldModel();

                if (mapFieldValue.containsKey(sourceEntity.getQueTitle())){
                    QingEditDataFieldValueModel addDataFieldValueModel=new QingEditDataFieldValueModel();

                    addDataFieldValueModel.setValue(mapFieldValue.get(sourceEntity.getQueTitle()));
                    List<QingEditDataFieldValueModel> listValues=new ArrayList<>();
                    listValues.add(addDataFieldValueModel);
                    addDataFieldModel.setQueId(sourceEntity.getQueId());
                    addDataFieldModel.setValues(listValues);
                }

                listFieldModels.add(addDataFieldModel);
            }

            qingEditDataModel.setAnswers(listFieldModels);


            //转字符串
            //String strJson= JSON.toJSONString(qingEditDataModel);
            Gson gson = new Gson();
            String strJson=gson.toJson(qingEditDataModel);
            //获取返回结果
            QingAddDataResultMain resultMain= QingflowAPIUtils.UpdDataToQing(qqingflowApiInfo,appKey,strJson,strQingAppId);

            if (resultMain==null){

                commonBackResult.setErrCode(-10015);
                commonBackResult.setErrMsg("服务器返回内容为空！");


            }
            else{
                if (resultMain.getErrCode()==0){
                    commonBackResult.setErrCode(resultMain.getErrCode());
                    commonBackResult.setErrMsg("提交成功！");
                    commonBackResult.setBackValue(resultMain.getResult().getRequestId());

                    //获取查询值的结果
                    String strRequestId=resultMain.getResult().getRequestId();
                    try{
                        Thread.sleep(500);
                    }
                    catch (Exception e){
                        e.printStackTrace();
                    }
                    QingQueryActionResultBase actionResultBase= QingflowAPIUtils.GetRequestResult(qqingflowApiInfo,strRequestId);
                    if (actionResultBase==null){
                        commonBackResult.setErrCode(-10066);
                        commonBackResult.setErrMsg("提交成功：获取提交结果的内容为空！");

                    }
                    else{
                        if (actionResultBase.getErrorCode()==0){

                            commonBackResult.setErrCode(0);
                            commonBackResult.setErrMsg("提交成功！");
                            String strValue=actionResultBase.getResult();
                            if (strValue==null||strValue.isEmpty()){

                            }
                            else{
                                Long lValue=Long.parseLong(actionResultBase.getResult());
                                commonBackResult.setBackLongValue(lValue);

                            }

                        }
                        else{

                            commonBackResult.setErrCode(actionResultBase.getErrorCode());
                            commonBackResult.setErrMsg(actionResultBase.getMessage());

                        }
                    }



                }
                else{
                    commonBackResult.setErrCode(resultMain.getErrCode());
                    commonBackResult.setErrMsg(resultMain.getErrMsg());

                }

            }

        }
        catch (Exception e){
            e.printStackTrace();
            //throw  e;
            commonBackResult.setErrCode(-10025);
            commonBackResult.setErrMsg("出错:"+e.getMessage()+"\n"+e.getStackTrace());
        }
        return commonBackResult;


    }

}
